package com.qqtop10.manager.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

public class LogHelper {
	public static void Log(String UserCode,String ServiceName,String FunName,String OperIP,String OperName,String ErrorMessage,Boolean IsSucc) {
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		
		 StringBuilder sb = new StringBuilder();
        
		 sb.append("UserCode="+UserCode+"&");
		 sb.append("ServiceName="+ServiceName+"&");
		 sb.append("FunName="+FunName+"&");
		 sb.append("OperIP="+OperIP+"&");
		 sb.append("OperName="+OperName+"&");
		 sb.append("ErrorMessage="+ErrorMessage+"&");
		 sb.append("IsSucc="+IsSucc+"&");
		 sb.append("AppID=2&");
		 sb.append("OperID=9647E109-7104-9DFE-D528-9880A691C0AD&");
		 sb.append("OperTime="+df.format(new Date())+"&");
		 
		 sb.append("Token=rMdlm4vfBtKuTbz88w*r3Rqvylu052fZ");
		 
		HttpRequest.sendPost("http://120.25.126.7/LogRecord/Add", sb.toString());
	
	}


	public static String getLocalIp(HttpServletRequest request) {
        String remoteAddr = request.getRemoteAddr();
        String forwarded = request.getHeader("X-Forwarded-For");
        String realIp = request.getHeader("X-Real-IP");

        String ip = null;
        if (realIp == null) {
            if (forwarded == null) {
                ip = remoteAddr;
            } else {
                ip = remoteAddr + "/" + forwarded.split(",")[0];
            }
        } else {
            if (realIp.equals(forwarded)) {
                ip = realIp;
            } else {
                if(forwarded != null){
                    forwarded = forwarded.split(",")[0];
                }
                ip = realIp + "/" + forwarded;
            }
        }
        return ip;
    }

}
